package com.message.Baidu_ai_API.controller;

import com.message.Baidu_ai_API.config.ExternalResourceConfig;
import com.message.Baidu_ai_API.service.AiPictureService;
import com.message.Baidu_ai_API.util.FileSetUtil;
import com.base.annotation.SysLog;
import com.base.helper.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

@Api(tags = "百度ai调用")
@RestController
@RequestMapping("/AiPicture")
public class AiPictureController {
    @Value("${external.resource.dir}")
    private String externalResourceDir;
    @Autowired
    private AiPictureService aiPictureService;
    @Autowired
    private ExternalResourceConfig externalResourceConfig;
    @SysLog(action = "AiPicture:Colourize", value = "ai图像增强：黑白图片上色")
    @ApiOperation(value = "黑白图片上色",notes = "ai图像增强")
    @RequestMapping(value="Colourize",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成base64的txt文件，用以转储
            @ApiImplicitParam(name = "image", value = "图片文件，必须是图片才行，pngjpg皆可，后缀名不限", paramType = "query")})
    public ResponseEntity<FileSystemResource> Colourize(@RequestParam("userid")Integer id, @RequestParam("image") MultipartFile image) throws Exception {
        String fileName= FileSetUtil.resetFileName(id,externalResourceDir,image);//生成的filename无后缀
        File file=FileSetUtil.saveFile(image,externalResourceDir,fileName);//生成的file为png文件
        String filePath=aiPictureService.Colourize(fileName,externalResourceDir);//转码传输到baiduapi后写入fileName+.txt,所返回的filePath直接指向对应txt文件
        aiPictureService.Base64ToImage(fileName,filePath,externalResourceDir);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new FileSystemResource(file));
    }
    @SysLog(action = "AiPicture:styleTrans", value = "ai图像增强：更换图片风格")
    @ApiOperation(value = "更换图片风格",notes = "ai图像增强")
    @RequestMapping(value="styleTrans",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成base64的txt文件，用以转储
            @ApiImplicitParam(name = "image", value = "图片文件，必须是图片才行，pngjpg皆可，后缀名不限", paramType = "query"),
            @ApiImplicitParam(name = "options", value = "图片转换的风格,1:卡通画,2:铅笔风格,3:彩色铅笔画,4:彩色糖块油画,5:神奈川冲浪里油画,6:薰衣草油画,7:奇异油画,8:呐喊油画,9:哥特油画。", paramType = "query")})
    public ResponseEntity<FileSystemResource> styleTrans(@RequestParam("userid")Integer id, @RequestParam("image") MultipartFile image, @RequestParam("options") Integer options) throws Exception {
        String fileName= FileSetUtil.resetFileName(id,externalResourceDir,image);//生成的filename无后缀
        File file=FileSetUtil.saveFile(image,externalResourceDir,fileName);//生成的file为png文件
        System.out.println(fileName+"//////////"+externalResourceDir);
        String filePath=aiPictureService.styleTrans(fileName,externalResourceDir,options);//转码传输到baiduapi后写入fileName+.txt,所返回的filePath直接指向对应txt文件
        aiPictureService.Base64ToImage(fileName,filePath,externalResourceDir);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new FileSystemResource(file));
    }
    @SysLog(action = "AiPicture:selfie_anime", value = "ai图像增强：人像动漫化")
    @ApiOperation(value = "人像动漫化",notes = "ai图像增强")
    @RequestMapping(value="selfie_anime",method = {RequestMethod.POST})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "id", paramType = "query"),//用户id，用于生成base64的txt文件，用以转储
            @ApiImplicitParam(name = "image", value = "图片文件，必须是图片才行，pngjpg皆可，后缀名不限", paramType = "query")})
    public ResponseEntity<FileSystemResource> selfie_anime(@RequestParam("userid")Integer id, @RequestParam("image") MultipartFile image) throws Exception {
        String fileName= FileSetUtil.resetFileName(id,externalResourceDir,image);//生成的filename无后缀
        File file=FileSetUtil.saveFile(image,externalResourceDir,fileName);//生成的file为png文件
        System.out.println(fileName+"//////////"+externalResourceDir);
        String filePath=aiPictureService.selfie_anime(fileName,externalResourceDir);//转码传输到baiduapi后写入fileName+.txt,所返回的filePath直接指向对应txt文件
        aiPictureService.Base64ToImage(fileName,filePath,externalResourceDir);
        HttpHeaders headers = new HttpHeaders();
        headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName());
        return ResponseEntity.ok()
                .headers(headers)
                .contentLength(file.length())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(new FileSystemResource(file));
    }
}
